﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmSearchProductID : Form
    {
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        TextBox tx;
        public frmSearchProductID(TextBox tx)
        {
            InitializeComponent();
            this.tx = tx;
            //this.tx1 = tx1;
        }

        private void btn_替代用料_材編_查詢_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string sp = "sp_產品_查詢";
                SqlDataAdapter cmd = new SqlDataAdapter(sp,con);
                cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                if (txt材編_替代用料.Text!="") cmd.SelectCommand.Parameters.AddWithValue("@材編或規格或品名", txt材編_替代用料.Text);
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                dataGV產品清單.DataSource = dt;
                PublicClass.setRowNumber(dataGV產品清單);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btn產品選擇_確定_Click(object sender, EventArgs e)
        {
            int r = dataGV產品清單.CurrentCell.RowIndex;
            tx.Text = dataGV產品清單.Rows[r].Cells[0].Value.ToString();
            this.Close();
            //if (tx.Name == "txt替代用料_替代用料") dataGV產品清單.Rows[r].Cells[1].Value.ToString();
        }

        private void frmSearchProductID_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string sp = "sp_產品_查詢";
                SqlDataAdapter cmd = new SqlDataAdapter(sp, con);
                cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                //cmd.SelectCommand.Parameters.AddWithValue("@材編或規格或品名", txt材編_替代用料.Text);
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                dataGV產品清單.DataSource = dt;
                PublicClass.setRowNumber(dataGV產品清單);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void dataGV產品清單_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV產品清單);
        }

        private void dataGV產品清單_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV產品清單);
        }
    }
}
